package com.ying.rabbitmqspringbootproducer.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * ClassName: OrderConfig
 * Package: com.ying.rabbitmqspringbootproducer.config
 * Description:
 *
 * @Author 刘英杰
 * @Create 2025/7/26 20:44
 * @Version 1.0
 */

@Configuration
public class OrderConfig {

    public static final String EXCHANGE_ORDER_NAME = "orderExchange";
    public static final String QUEUE_ORDER_NAME = "orderQueue";

    public static final String QUEUE_ORDERDL_NAME = "orderDLQueue";

    @Bean
    public Exchange orderExchange() {
        return ExchangeBuilder.topicExchange(EXCHANGE_ORDER_NAME).durable(true).build();
    }

    @Bean
    public Queue orderQueue() {

        return QueueBuilder.durable(QUEUE_ORDER_NAME)
                .withArgument("x-dead-letter-exchange",DLXConfig.EXCHANGE_DLX_NAME)
                .withArgument("x-dead-letter-routing-key","order.delay.orderId")
                .withArgument("x-message-ttl",10000)
                .build();
    }

    @Bean
    public Binding bindingOrderQueue(Exchange orderExchange, Queue orderQueue) {
        return BindingBuilder.bind(orderQueue).to(orderExchange).with("order.#").noargs();
    }

    @Bean
    public Queue orderDLQueue() {
        return QueueBuilder.durable(QUEUE_ORDERDL_NAME).build();
    }

    @Bean
    public Binding bindingOrderDLQueue(Exchange dlxExchange, Queue orderDLQueue) {
        return BindingBuilder.bind(orderDLQueue).to(dlxExchange).with("order.delay.#").noargs();
    }


}
